package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AlertDataHandleDto;
import com.zzyl.dto.AlertDataPageQueryDto;
import com.zzyl.entity.AlertData;
import com.zzyl.intercept.AutoFillInterceptor;
import com.zzyl.mapper.AlertDataMapper;
import com.zzyl.mapper.UserMapper;
import com.zzyl.service.AlertDataService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.AlertDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description AlertDataServiceImpl
 * @Author 聆听
 * @Date 2025-09-15
 */
@Service
public class AlertDataServiceImpl implements AlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResponse<AlertDataVo> FindByPage(AlertDataPageQueryDto alertDataPageQueryDto) {
        PageHelper.startPage(alertDataPageQueryDto.getPageNum(), alertDataPageQueryDto.getPageSize());
        Long currentUserId = AutoFillInterceptor.loadUserId();
        Page<AlertDataVo> alertDataVoPage = alertDataMapper.selectByPage(alertDataPageQueryDto, currentUserId);
        return PageResponse.of(alertDataVoPage, AlertDataVo.class);
    }

    @Override
    public void updateAlertData(Long id, AlertDataHandleDto alertDataHandleDto) {
        // 获取当前用户id
        Long userId = AutoFillInterceptor.loadUserId();
        AlertData alertData = alertDataMapper.selectByPrimaryKey(id);
        alertData.setProcessorId(userId);
        alertData.setStatus(1);
        alertData.setProcessorName(userMapper.selectByPrimaryKey(userId).getUsername());
        alertData.setProcessingResult(alertDataHandleDto.getProcessingResult());
        alertData.setProcessingTime(alertDataHandleDto.getProcessingTime());
        alertData.setUpdateBy(userId);

        alertDataMapper.updateByPrimaryKey(alertData);
        alertDataMapper.updateBySameData(alertData);
    }
}
